package com.vueblog.webapi.controller;


import com.vueblog.webapi.exception.BusinessException;
import com.vueblog.webapi.request.CreateCommentInfoRequest;
import com.vueblog.webapi.request.CreateOrUpdateTypeRequest;
import com.vueblog.webapi.response.BaseResponse;
import com.vueblog.webapi.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author luosj
 * @since 2021-05-15
 */
@Controller
@CrossOrigin
@RequestMapping("/show/comments")
public class ShowCommentController extends BaseController {

    @Autowired
    private ICommentService commentService;

    /**
     * 根据博客id获评论信息
     */
    @GetMapping("/byBlogId/{id}")
    @ResponseBody
    public BaseResponse getCommentsByBlogId(@PathVariable("id") Long id) {
        return commentService.getCommentsByBlogId(id);
    }

    /**
     * 创建评论
     */
    @PostMapping("/create")
    @ResponseBody
    public BaseResponse createComment(@RequestBody @Valid CreateCommentInfoRequest request, BindingResult bindingResult){
        checkBindingResult(bindingResult);
        try {
            return commentService.createComment(request);
        } catch (BusinessException e) {
            return new BaseResponse(-1, e.getMsg());
        }
    }

    /**
     * 删除评论
     */
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public BaseResponse deleteComment(@PathVariable("id") String id) {
        return commentService.deleteComment(id);
    }
}
